import {
  Module,
  NestModule,
  MiddlewareConsumer,
  RequestMethod,
} from '@nestjs/common';
import { GirlController } from './girl.controller';
import { GirlService } from './girl.service';
import { TypeOrmModule } from '@nestjs/typeorm';
import { Girl } from './entities/girl.entity';
import { AppModule } from '../app.module';
import { InterceptorMiddleware } from '../interceptor/interceptor.middleware';

@Module({
  imports: [TypeOrmModule.forFeature([Girl])],
  controllers: [GirlController],
  providers: [GirlService],
})

// 要使用中间件，就要加上 implements NestModule
export class GirlModule implements NestModule {
  // 记得把NestModule、MiddlewareConsumer从@nestjs/common中import过来
  configure(consumer: MiddlewareConsumer) {
    // 这样处理后，以后/girls路径下的所有接口都会先走InterceptorMiddleware，如果InterceptorMiddleware不放行，那请求就不能继续
    consumer.apply(InterceptorMiddleware).forRoutes('/girls');

    // forRoutes方法也可以传一个对象，像这样就是所有/girls路径下的接口中的get请求会被InterceptorMiddleware拦截
    // consumer
    //   .apply(InterceptorMiddleware)
    //   .forRoutes({ path: 'girls', method: RequestMethod.GET });
  }
}
